package publish

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-iot-common/iotRemote"
	"time"
)

func Scheduler() {
	go recordScheduler()
	go redisScheduler()
}
func recordScheduler() {
	for true {
		time.Sleep(1 * time.Second)
		if !iotRemote.Mqtt.Client.IsConnected() {
			continue
		}

		pushRecord()
	}
}

func redisScheduler() {
	for true {
		time.Sleep(1 * time.Second)
		if !iotRemote.Mqtt.Client.IsConnected() {
			continue
		}

		pushRedis()
	}
}

func pushRedis() {
	// 高频变化的设备数值类实体
	configEntityPublish.publish(edgeEntity.Type.ConfigEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.ChannelEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.ChannelStatusEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.DeviceEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.DeviceMapperEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.DeviceModelEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.DeviceStatusEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.DeviceValueExTaskEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.ExtendConfigEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.IotDeviceModelEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.OperateChannelTaskEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.OperateEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.OperateManualTaskEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.OperateMonitorTaskEntity.Type())
	configEntityPublish.publish(edgeEntity.Type.RepoCompEntity.Type())

	// 高频变化的设备数值类实体
	valueEntityPublish.publish(edgeEntity.Type.DeviceValueEntity.Type())
	valueEntityPublish.publish(edgeEntity.Type.DeviceValueExEntity.Type())
}

func pushRecord() {
	// 推送数据库记录
	recordEntityPublish.publish(edgeEntity.Type.DeviceRecordEntity.Type())
	recordEntityPublish.publish(edgeEntity.Type.OperateRecordEntity.Type())
}
